<?php
namespace api\toilet\controller;

use api\admin\model\FeedbackModel;
use api\admin\model\FeedbackRecordModel;
use api\admin\model\ToiletModel;
use cmf\controller\RestBaseController;
use think\Db;

class FeedbackController extends RestBaseController
{

    /**
     * [getFeedbackDetail 获取当前公厕详情]
     * @return [type] [description]
     */
    public function getFeedbackDetail()
    {

        $param = $this->request->param();
        if (!isset($param['id']))
        {
            $this->error('id不能为空');
        }
        $feedback = new FeedbackModel();
        $detail   = $feedback->getFeedBackDetail($param['id']);
        if (!empty($detail['thumbnail']))
        {
            $thumbnail = [];
            foreach ($detail['thumbnail'] as $key => $value)
            {
                $thumbnail[$key] = cmf_get_domain() . '/upload/' . $value;
            }
            $detail['thumbnail'] = $thumbnail;
        }
        if ($detail)
        {
            $detail['satisfaction'] = config('satisfaction');
        }

        $this->success('获取成功', $detail);

    }

    /**
     * [addFeedback 添加评价]
     */
    public function addFeedback()
    {

        $param = $this->request->param();

        if (empty($param['id']) || empty($param['t_id']))
        {
            $this->error('参数异常');
        }

        // if (empty($param['tags'])) {
        //     $this->error('请选择投诉建议');
        // }

        $record = new FeedbackRecordModel();
        $res    = $record->addFeedBackRecord($param);

        if ($res)
        {
            $this->success('提交成功,感谢您的建议');
        }
        else
        {
            $this->error('提交失败,请稍后再试');
        }
    }

    //
    public function getSatisfaction()
    {

        $mac = $this->request->param('mac');

        if ($mac)
        {
            $Toilet    = new ToiletModel();
            $toiletRes = $Toilet->getToiletFields(['mac' => $mac, 'status' => 1]);
        }
        else
        {
            $this->error('mac不能为空');
        }
        if (!empty($toiletRes))
        {

            $record = new FeedbackRecordModel();

            $detail['best'] = $record->where(['t_id' => $toiletRes['id'], 'satisfaction' => 1])->count();
            $detail['good'] = $record->where(['t_id' => $toiletRes['id'], 'satisfaction' => 2])->count();
            $detail['bad']  = $record->where(['t_id' => $toiletRes['id'], 'satisfaction' => 3])->count();
        }
        else
        {
            $detail = [
                'best' => 0,
                'good' => 0,
                'bad' => 0,
            ];
        }

        $this->success('获取成功', $detail);
    }

}
